package redis

import (
	"cart_srv/cart"
	"cart_srv/model/mysql"
	"encoding/json"
	"strconv"
)

func SyncGoodsStock(goodsId int64, stock int) {
	key := "goods_stock:goods_id_" + strconv.Itoa(int(goodsId))
	for i := 0; i < stock; i++ {
		Rdb.LPush(key, goodsId)
	}
}

// 判断库存是否充足
func GetGoodsLen(goodsId int64) int64 {
	key := "goods_stock:goods_id_" + strconv.Itoa(int(goodsId))
	return Rdb.LLen(key).Val()
}

// 判断商品是否存在
func GoodsIsExists(userId int64, goodsId int64) bool {
	key := "cart:user_id_" + strconv.Itoa(int(userId)) + ":goods_id_" + strconv.Itoa(int(goodsId))
	return Rdb.HExists(key, "GoodsId").Val()
}

// 哈希添加购物车
func SyncCart(cart *mysql.GoodsCart) error {
	key := "cart:user_id_" + strconv.Itoa(cart.UserId) + ":goods_id_" + strconv.Itoa(cart.GoodsId)
	marshal, _ := json.Marshal(cart)
	var m map[string]interface{}
	json.Unmarshal(marshal, &m)
	return Rdb.HMSet(key, m).Err()
}

// 修改购物车中数量
func UpdateGoodsNum(userId int64, goodsId int64, num int64) {
	key := "cart:user_id_" + strconv.Itoa(int(userId)) + ":goods_id_" + strconv.Itoa(int(goodsId))
	Rdb.HIncrBy(key, "GoodsNum", num)
}

// 删除购物车中商品
func DelGoods(userId int64, goodsId int64) {
	key := "cart:user_id_" + strconv.Itoa(int(userId)) + ":goods_id_" + strconv.Itoa(int(goodsId))
	Rdb.Del(key)
}

// 购物车列表
func CartList(userId int64) (cartlist []*cart.CartList) {
	key := "cart:user_id_" + strconv.Itoa(int(userId)) + ":*"
	keyMap := Rdb.Keys(key).Val()
	if len(keyMap) == 0 {
		return nil
	}
	for _, v := range keyMap {
		goodsMap := Rdb.HGetAll(v).Val()
		goodsId, _ := strconv.Atoi(goodsMap["GoodsId"])
		goodsNum, _ := strconv.Atoi(goodsMap["GoodsNum"])
		goodsPrice, _ := strconv.ParseFloat(goodsMap["GoodsPrice"], 64)
		cartlist = append(cartlist, &cart.CartList{
			UserId:     userId,
			GoodsId:    int64(goodsId),
			GoodsName:  goodsMap["GoodsName"],
			GoodsPrice: float32(goodsPrice),
			GoodsCover: goodsMap["GoodsCover"],
			GoodsNum:   int64(goodsNum),
		})
	}
	return
}

// 查询
func Detail(userId int64, goodsId int64) (cartlist []*cart.CartList) {
	key := "cart:user_id_" + strconv.Itoa(int(userId)) + ":goods_id_" + strconv.Itoa(int(goodsId))
	keyMap := Rdb.Keys(key).Val()
	if len(keyMap) == 0 {
		return nil
	}
	for _, v := range keyMap {
		goodsMap := Rdb.HGetAll(v).Val()
		goodsNum, _ := strconv.Atoi(goodsMap["GoodsNum"])
		goodsPrice, _ := strconv.ParseFloat(goodsMap["GoodsPrice"], 64)
		cartlist = append(cartlist, &cart.CartList{
			UserId:     userId,
			GoodsId:    goodsId,
			GoodsName:  goodsMap["GoodsName"],
			GoodsPrice: float32(goodsPrice),
			GoodsCover: goodsMap["GoodsCover"],
			GoodsNum:   int64(goodsNum),
		})
	}
	return
}

// 计算总数，总价格
func Sum(userId int64) (count int, amount float64) {
	key := "cart:user_id_" + strconv.Itoa(int(userId)) + ":*"
	keyMap := Rdb.Keys(key).Val()
	if len(keyMap) == 0 {
		return 0, 0
	}
	for _, v := range keyMap {
		goodsMap := Rdb.HGetAll(v).Val()
		goodsNum, _ := strconv.Atoi(goodsMap["GoodsNum"])
		goodsPrice, _ := strconv.ParseFloat(goodsMap["GoodsPrice"], 64)
		count += goodsNum
		amount += goodsPrice * float64(goodsNum)
	}
	return
}

// 清空购物车
func CartFlush(usrId int64) {
	key := "cart:user_id_" + strconv.Itoa(int(usrId)) + ":*"
	keyMap := Rdb.Keys(key).Val()
	for _, v := range keyMap {
		Rdb.Del(v)
	}
}
func CartIsExists(userId int64) bool {
	key := "cart:user_id_" + strconv.Itoa(int(userId)) + ":*"
	return Rdb.HExists(key, "UserId").Val()
}
func Checked(userId int64, check string) {
	key := "cart:user_id_" + strconv.Itoa(int(userId)) + ":*"
	Rdb.HMGet(key, check)
}
